<?php
class commonService extends Service{
    
	public function __construct(){
		parent::__construct();
	}
    //[nextpage]
    public function SpLongBody($mybody, $spsize, $sptag)
    {
        if(strlen($mybody) < $spsize)
        {
            return $mybody;
        }
        $mybody = stripslashes($mybody);
        $bds = explode('<', $mybody);
        $npageBody = '';
        $istable = 0;
        $mybody = '';
        foreach($bds as $i=>$k)
        {
            if($i==0)
            {
                $npageBody .= $bds[$i]; continue;
            }
            $bds[$i] = "<".$bds[$i];
            if(strlen($bds[$i])>6)
            {
                $tname = substr($bds[$i],1,5);
                if(strtolower($tname)=='table')
                {
                    $istable++;
                }
                else if(strtolower($tname)=='/tabl')
                {
                    $istable--;
                }
                if($istable>0)
                {
                    $npageBody .= $bds[$i]; continue;
                }
                else
                {
                    $npageBody .= $bds[$i];
                }
            }
            else
            {
                $npageBody .= $bds[$i];
            }
            if(strlen($npageBody)>$spsize)
            {
                $mybody .= $npageBody.$sptag;
                $npageBody = '';
            }
        }
        if($npageBody!='')
        {
            $mybody .= $npageBody;
        }
        //return addslashes($mybody);
        return $mybody;
    }
    
    public function pageBreak($content){ 
        $pattern  = "/\[nextpage\]/";
        $strSplit = preg_split($pattern, $content, -1, PREG_SPLIT_NO_EMPTY); 
        $count    = count($strSplit); 
        $outStr   = ""; 
        $i        = 1; 
     
        if ($count > 1 ) { 
            $outStr   = "<div id='page_break'>"; 
            foreach($strSplit as $value) { 
                if ($i <= 1) { 
                    $outStr .= "<div id='page_$i'>$value</div>"; 
                } else { 
                    $outStr .= "<div id='page_$i' class='collapse'>$value</div>"; 
                } 
                $i++; 
            } 
     
            $outStr .= "<div class='num'>"; 
            for ($i = 1; $i <= $count; $i++) { 
                $outStr .= "<li>$i</li>"; 
            } 
            $outStr .= "</div></div>"; 
            return $outStr; 
        } else { 
            return $content; 
        } 
    } 
    
    public function get_cat_detail($catid){
        $table=table('category');
        $query="select * from `{$table}` where `id`='{$catid}' limit 1";
		return $this->m->select($query);
    }
    /*
            $query="select `id`,`typeid`,`typeid2`,`title`,`description` from `#@__archives` where `typeid`='{$catid}' and arcrank>-1 order by `sortrank` desc ";
			$total=$do->_total($query);
			$ret=$do->_page($total,10);
			$pager=$do->pager($ret);
			
			
			$query=$query." limit {$ret['c']}";

			$arc_list=$do->get_arc_list_pager($query);
			$arc_list=(array) $arc_list;
			
			unset($total,$ret);
    */
    public function _total($query){
		$query=preg_replace("/([\w\W]*?select)([\w\W]*?)(from[\w\W]*?)/i","$1 count(*) as `t` $3",$query);
		return $this->m->get_field($query);
	}
	public function _page($total,$size){
		$ret=array(
			'total'=>0,
			'total_page'=>0,
			'page'=>-1,
			'pre_page'=>-1,
			'next_page'=>-1,
			'a'=>0,
			'b'=>0,
			'c'=>'0,0',
			'range_1'=>0,
			'range_2'=>0,
			'pe'=>array(),
		);
		if($total==0) return $ret;
		$ret['total']=$total;
		$page=intval(isset($_GET['page'])?$_GET['page']:1);
		$total_page=ceil($total/$size);
		$ret['total_page']=$total_page;
		
		$page=max($page,1);
		$page=min($page,$total_page);
		$ret['page']=$page;
		
		$pre_page=($page==1)?-1:$page-1;//特殊情况-1
		$ret['pre_page']=$pre_page;
		
		$next_page=($page==$total_page)?-1:$page+1;//特殊情况-1
		$ret['next_page']=$next_page;
		
		$ret['a']=($page-1)*$size;
		$ret['b']=$size;
		$ret['c']=$ret['a'].','.$ret['b'];
		
		$ret['range_1']=$ret['total']?$ret['a']+1:0;
		$ret['range_2']=$ret['total']?$ret['a']+$size:0;
		$ret['pe']=range(1,$total_page);
		return $ret;
	}
	public function pager($ret,$url=''){
		$add='';
		$p_url=$_SERVER['QUERY_STRING'];
		parse_str($p_url,$arr);
		if(array_key_exists('page',$arr)){
			unset($arr['page']);
		}
		if(is_array($arr)){
			if(count($arr)>0){
				$add=$url.'?'.http_build_query($arr).'&';
			}else{
				$add=$url.'?';
			}
		}
		$return=array();
		
		if($ret['page']==1){
			//$return[]=" 首页 ";
		}else{
			//$return[]=" <a href='{$add}page=1'>[首页]</a> ";
		}
		if($ret['pre_page']==-1){
			$return[]=" 上一页";
		}else{
			$return[]=" <a href='{$add}page={$ret['pre_page']}'>[上一页]</a> ";
		}
		if($ret['next_page']==-1){
			$return[]=" 下一页";
		}else{
			$return[]=" <a href='{$add}page={$ret['next_page']}'>[下一页]</a> ";
		}
		if($ret['page']==$ret['total_page']){
			//$return[]=" 尾页 ";
		}else{
			//$return[]=" <a href='{$add}page={$ret['total_page']}'>[尾页]</a> ";
		}
		$return[]=" {$ret['page']}/{$ret['total_page']} ";
		//$return[]=" <br />每页显示 {$ret['b']} 条";
		
		$next=$ret['next_page']==-1?$ret['total_page']:$ret['next_page'];
		
		$return[]=" 跳至第 <input type='text' id='pagejump' value='{$next}' style='width:20px;' /> 页";
		$return[]=" <input type='button' value='GO!'   style='margin-left:8px'onclick='location.href=\"{$add}page=\"+document.getElementById(\"pagejump\").value' /> ";
		return implode('',$return);
	}
}